package com.link.gateway.core.redis.redisclient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanResult;

import java.util.*;

/**
 * @author gsan
 * @create 2019/3/28
 */
public class LinkRedisClusterClient implements LinkRedisClient {

    private  JedisCluster jedisCluster;

    public LinkRedisClusterClient(JedisCluster jedisCluster) {
        this.jedisCluster = jedisCluster;
    }

    @Override
    public String get(String key) {
        return jedisCluster.get(key);
    }

    @Override
    public String hget(String key, String propertyKey) {
        return jedisCluster.hget(key,propertyKey);
    }

    @Override
    public void close() {
        /**
         * 此方法会导致此服务无法连接redis集群；
         * 调用此方法后，获取redis连接会产生如下异常：JedisNoReachableClusterNodeException: No reachable node in cluster
         */
        //jedisCluster.close();
    }

    @Override
    public Long ttl(String s) {
        return jedisCluster.ttl(s);
    }

    @Override
    public Long expire(String s, Integer tokenExpire) {
        return jedisCluster.expire(s,tokenExpire);
    }

    @Override
    public boolean exists(String loginInfoMapKey) {
        return jedisCluster.exists(loginInfoMapKey);
    }


    @Override
    public List<String> hmget(String key, String... fields) {
        return jedisCluster.hmget(key,fields);
    }

    @Override
    public Long hdel(String loginInfoMapKey, String key) {
        return jedisCluster.hdel(loginInfoMapKey,key);
    }

    @Override
    public Long hset(String loginInfoMapKey, String key, String value) {
        return jedisCluster.hset(loginInfoMapKey,key,value);
    }

    @Override
    public String hmset(String mapKey, Map<String, String> map) {
        return jedisCluster.hmset(mapKey,map) ;
    }

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key,value);
    }

    @Override
    public String set(String key, String value, String nxxx, String expx, int time) {
        return jedisCluster.set(key,value,nxxx,expx,time);
    }

    @Override
    public List<String> brpop(int i, String key) {
        return jedisCluster.brpop(i,key);
    }

    @Override
    public long llen(String key) {
        return jedisCluster.llen(key);
    }

    @Override
    public Long lpush(String key, String value) {
        return jedisCluster.lpush(key,value) ;
    }

    @Override
    public Long sadd(String key, String[] value) {
        return jedisCluster.sadd(key,value);
    }

    @Override
    public long scard(String key) {
        return jedisCluster.scard(key);
    }

    @Override
    public String spop(String key) {
        return jedisCluster.spop(key);
    }

    /**
     * 此方法为自定义方法，模拟jedis的keys
     * @param pattern
     * @return
     */
    @Override
    public Set<String> keys(String pattern) {
        TreeSet<String> keys = new TreeSet<>();
        Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
        for (String k : clusterNodes.keySet()) {
            JedisPool jp = clusterNodes.get(k);
            Jedis connection = jp.getResource();
            try {
                keys.addAll(connection.keys(pattern));
            } catch (Exception e) {
                throw e;
            } finally {
                connection.close();//用完一定要close这个链接！！！
            }
        }
        return keys;
    }

    @Override
    public Set<byte[]> keys(byte[] pattern) {
        Set<byte[]> bytes = new HashSet<>();
        Set<String> valueSet = keys(new String(pattern));
        for (String value : valueSet ) {
            bytes.add(value.getBytes());
        }
        return bytes;
    }

    @Override
    public Long del(String[] keyStrs) {
        int tmpValue = 0;
        for (String key : keyStrs ) {
            del(key);
            tmpValue ++;
        }
        return (long)tmpValue;
    }

    @Override
    public Long del(String mutexKey) {
        return jedisCluster.del(mutexKey);
    }

    @Override
    public String setex(String mutexKey, int i, String y) {
        return jedisCluster.setex(mutexKey,i,y);
    }

    @Override
    public String select(int dbIndex) {
        return jedisCluster.select(0);
    }

    @Override
    public Set<String> hkeys(String var1) {
        return jedisCluster.hkeys(var1);
    }

    @Override
    public List<byte[]> mget(byte[][] keys) {
        List<byte[]> bytes = new ArrayList<>();
        if (keys == null || keys.length <= 0) {
            return bytes;
        }
        for (int i = 0; i < keys.length; i++) {
            byte[] byteResult = jedisCluster.get(keys[i]);
            bytes.add(byteResult);
        }
        return bytes;
    }

    @Override
    public Map<String, String> hgetAll(String key) {
        return jedisCluster.hgetAll(key);
    }

    @Override
    public List<String> lrange(String key, long start, long end) {
        return jedisCluster.lrange(key,start,end);
    }

    @Override
    public String ltrim(String key, long start, long end) {
        return jedisCluster.ltrim(key,start,end);
    }

    @Override
    public String lindex(String key, long index) {
        return jedisCluster.lindex(key,index);
    }

    @Override
    public Long pexpire(String key, long milliseconds) {
        return jedisCluster.pexpire(key,milliseconds);
    }

    @Override
    public Long hincrBy(String key, String field, long value) {
        return jedisCluster.hincrBy(key,field,value);
    }

    @Override
    public Double hincrByFloat(String key, String field, double value) {
        return jedisCluster.hincrByFloat(key,field,value);
    }

    @Override
    public Long setnx(String key, String value) {
        return jedisCluster.setnx(key,value);
    }

    @Override
    public String getSet(String key, String value) {
        return jedisCluster.getSet(key,value);
    }

    @Override
    public String rpop(String key) {
        return jedisCluster.rpop(key);
    }

    @Override
    public Boolean scriptExists(String key,String sha1) {
        return jedisCluster.scriptExists(key,sha1);
    }

    @Override
    public String scriptLoad(String script,String key) {
        return jedisCluster.scriptLoad(script,key);
    }

    @Override
    public Object evalsha(String sha1, int keyCount, String... params) {
        return jedisCluster.evalsha(sha1, keyCount, params);
    }

    @Override
    public Boolean hexists(String key, String field) {
        return jedisCluster.hexists(key,field);
    }

    @Override
    public ScanResult<Map.Entry<String, String>> hscan(final String key, final String cursor) {
        return jedisCluster.hscan(key, cursor);
    }

    @Override
    public Object eval(String script, List<String> keys, List<String> args) {
        return jedisCluster.eval(script, keys, args);
    }

}
